package com.wanczy.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.wanczy.pojo.UserTable;
import com.wanczy.service.UserLoginService;
import com.wanczy.service.impl.UserLoginServiceImpl;
import com.wanczy.util.DateToString;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
@WebServlet("/servlet/queryUserByUserAccountAndState")
public class QueryUserByUserAccountAndState extends HttpServlet {
	private static final long serialVersionUID = 1L;
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		PrintWriter out = response.getWriter();
		String userAccount = request.getParameter("userAccount");
		int userState = Integer.parseInt(request.getParameter("userState"));
		int pageSize = Integer.parseInt(request.getParameter("pageSize"));
		int pageCurrent = Integer.parseInt(request.getParameter("pageCurrent"));
		
		UserLoginService service = new UserLoginServiceImpl();
		int count = service.findConutByUserAccountAndState(userAccount, userState);
		List<UserTable> list = service.findByUserAccountAndState(userAccount, userState, pageSize, pageCurrent);
		
		JSONArray jsonArray = new JSONArray();
		JSONObject jsonO = new JSONObject();
		jsonO.put("allCurrent", count);//����ѯ�����ݱ�������JSONArray�±�Ϊ0��λ��
		jsonArray.add(jsonO);
		Iterator<UserTable> it = list.iterator();
		while(it.hasNext()) {
			UserTable userTable = it.next();
			JSONObject jsonObject = new JSONObject();
			jsonObject.put("userId", userTable.getUser_id());
			jsonObject.put("userAccount",userTable.getUser_account());
			jsonObject.put("userSex",userTable.getUser_sex() == 1?"男":"女");
			jsonObject.put("userEmail",userTable.getUser_email());
			jsonObject.put("userAge",userTable.getUser_age());
			jsonObject.put("userPass",userTable.getUser_pass());
			jsonObject.put("userState",userTable.getUser_state() == 1?"在用":"ͣ禁用");
			jsonObject.put("userBirthday",DateToString.getString(userTable.getUser_birthday()));
			jsonArray.add(jsonObject);
		}
		out.println(jsonArray.toString());
		out.close();
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
